<?php

/**
 * @author Immanuel Bauer | 0626117
 */
class Message {
	
	/**
	 *  @var int 
	 */
	private $id;
	
	/** 
	 * @var int
	 */
	private $messageID;
	
	/**
	 *  @var int 
	 */
	private $listID;
	
	/**
	 *  @var int
	 */
	private $todoID;
	
	/** 
	 * @var int 
	 */
	private $userID;
	
	/**
	 *  @var int
	 */
	private $authorID;
	
	/**
	 *  @var string 
	 */
	private $title;
	
	/**
	 *  @var string
	 */
	private $text;
	
	/**
	 *  @var timestamp
	 */
	private $date;
	
	/**
	 * Creates a new object of the model and fills it with the given data.
	 * @param string $title
	 * @param string $text
	 * @param timestamp $date
	 * @param int $authorID
	 * @param int $messageID
	 * @param int $todoID
	 * @param int $listID
	 * @param int $userID
	 */
	function __construct($title, $text, $date, $authorID, $messageID = '', $todoID = '', $listID = '', $userID = '') {
		$this->id = null;
		$this->title = $title;
		$this->text = $text;
		$this->date = (int)$date;
		$this->authorID = (int)$authorID;
		$this->messageID = (int)$messageID;
		$this->listID = (int)$listID;
		$this->todoID = (int)$todoID;
		$this->userID = (int)$userID;
	}
	
	/**
	 * Fetches the message data for the given id and returns a Message object with the data.
	 * Returns null if the message doesn't exist.
	 * 
	 * @param int $id
	 * @return Message
	 */
	public static function getMessage($id){
		$qry = mysql_query('SELECT * FROM '.TABLE_PREFIX.'messages WHERE id = \''.mysql_real_escape_string($id).'\';');
		$row = mysql_fetch_assoc($qry);
		if(isset($row['id'])){
			$msg = new Message($row['title'], $row['text'], $row['date'], $row['author'], $row['linked_to_message'], $row['linked_to_todo'], $row['linked_to_list'], $row['linked_to_user']);	
			$msg->setID($row['id']);
			return $msg;
		} else {
			return null;
		}
	}
	
	/**
	 * Retrieves the full messages that are linked to the given id and returns them in an array.
	 * @param int $messageID
	 * @param int $todoID
	 * @param int $listID
	 * @param int $userID
	 * @param int $authorID
	 * @param int $from Default is 0
	 * @param int $rows Default is 30
	 * @param int $order Default is 'asc'. The	options are 'asc' or 'desc';
	 * @return array<Message>
	 */
	public static function getMessages($messageID = '', $todoID = '', $listID = '', $userID = '', $authorID = null, $from = 0, $rows = 30, $order = 'asc'){
		$out = array();
		
		if(!is_null($authorID)){
			$author = ' AND author = \''.$authorID.'\' ';
		}
		
		switch($order){
			case 'asc': $order = 'ASC'; break;
			case 'desc': $order = 'DESC'; break;
			case 'a': $order = 'ASC'; break;
			case 'd': $order = 'DESC'; break;
			default: $order = 'DESC'; break;
		}
		
		$qry = mysql_query('SELECT * FROM '.TABLE_PREFIX.'messages WHERE 
						linked_to_message = \''.mysql_real_escape_string($messageID).'\'
						AND linked_to_todo = \''.mysql_real_escape_string($todoID).'\'
						AND linked_to_list = \''.mysql_real_escape_string($listID).'\'
						AND linked_to_user = \''.mysql_real_escape_string($userID).'\'
						'.$author.' ORDER BY date '.$order.' LIMIT '.mysql_real_escape_string($from).','.mysql_real_escape_string($rows).';');
		while($row = mysql_fetch_assoc($qry)){
			$msg = new Message($row['title'], $row['text'], $row['date'], $row['author'], $row['linked_to_message'], $row['linked_to_todo'], $row['linked_to_list'], $row['linked_to_user']);	
			$msg->setID($row['id']);
			array_push($out, $msg);
		}
		return $out;
	}
	
	/**
	 * Retrieves the full messages that are linked to the given message and returns them in an array.
	 * @param int $messageID
	 * @param int $order Default is 'asc'. The	options are 'asc' or 'desc';
	 * @return array<Message>
	 */
	public static function getReplys($messageID, $order = 'asc'){
		$out = array();
		
		switch($order){
			case 'asc': $order = 'ASC'; break;
			case 'desc': $order = 'DESC'; break;
			case 'a': $order = 'ASC'; break;
			case 'd': $order = 'DESC'; break;
			default: $order = 'DESC'; break;
		}
		
		$qry = mysql_query('SELECT * FROM '.TABLE_PREFIX.'messages WHERE 
						linked_to_message = \''.mysql_real_escape_string($messageID).'\'
						ORDER BY date '.$order.';');
		while($row = mysql_fetch_assoc($qry)){
			$msg = new Message($row['title'], $row['text'], $row['date'], $row['author'], $row['linked_to_message'], $row['linked_to_todo'], $row['linked_to_list'], $row['linked_to_user']);	
			$msg->setID($row['id']);
			array_push($out, $msg);
		}
		return $out;
	}
	
	/**
	 * Retrieves the count of messages that match the given parameters.
	 * @param int $messageID
	 * @param int $todoID
	 * @param int $listID
	 * @param int $userID
	 * @param int $authorID
	 * @return int
	 */
	public static function getMessageCount($messageID = '', $todoID = '', $listID = '', $userID = '', $authorID = null){
		$out = array();
		
		if(!is_null($authorID)){
			$author = ' AND author = \''.$authorID.'\' ';
		}
		
		$count = mysql_fetch_row(mysql_query('SELECT count(*) FROM '.TABLE_PREFIX.'messages WHERE 
						linked_to_message = \''.mysql_real_escape_string($messageID).'\'
						AND linked_to_todo = \''.mysql_real_escape_string($todoID).'\'
						AND linked_to_list = \''.mysql_real_escape_string($listID).'\'
						AND linked_to_user = \''.mysql_real_escape_string($userID).'\'
						'.$author.';'));
		return (int)$count[0];
	}
	
	/**
	 * Saves the current state of the model object into the database
	 * @return boolean true if save was successfull
	 */
	public function saveToDB(){
		if(is_null($this->id)){
			//insert
			$qry = mysql_query('INSERT INTO '.TABLE_PREFIX.'messages (`id`, `linked_to_message`, `linked_to_list`, `linked_to_todo`, `linked_to_user`, `author`, `title`, `text`, `date`) VALUES (
				\'\',
				\''.mysql_real_escape_string($this->messageID).'\',
				\''.mysql_real_escape_string($this->listID).'\',
				\''.mysql_real_escape_string($this->todoID).'\',
				\''.mysql_real_escape_string($this->userID).'\',
				\''.mysql_real_escape_string($this->authorID).'\',
				\''.mysql_real_escape_string($this->title).'\',
				\''.mysql_real_escape_string($this->text).'\',
				\''.mysql_real_escape_string($this->date).'\'
			);');
			if($qry){
				$qry = mysql_query('SELECT id FROM '.TABLE_PREFIX.'messages
					 WHERE `linked_to_message` = \''.mysql_real_escape_string($this->messageID).'\'
					 	AND `linked_to_list` = \''.mysql_real_escape_string($this->listID).'\' 
					 	AND `linked_to_todo` = \''.mysql_real_escape_string($this->todoID).'\'
					 	AND `linked_to_user` = \''.mysql_real_escape_string($this->userID).'\'
					 	AND `author` = \''.mysql_real_escape_string($this->authorID).'\'
					 	AND `date` = \''.mysql_real_escape_string($this->date).'\'
				;');
				$row = mysql_fetch_assoc($qry);
				if(isset($row['id'])){
					$this->id = (int)$row['id'];
					return true;
				} else{
					return false;
				}
			} else {
				return false;
			}
		} else {
			//update
			$qry = mysql_query('UPDATE '.TABLE_PREFIX.'messages SET 
				`linked_to_message` = \''.mysql_real_escape_string($this->messageID).'\',
				`linked_to_list` = \''.mysql_real_escape_string($this->listID).'\',
				`linked_to_todo` = \''.mysql_real_escape_string($this->todoID).'\',
				`linked_to_user` = \''.mysql_real_escape_string($this->userID).'\',
				`author` = \''.mysql_real_escape_string($this->authorID).'\',
				`title` = \''.mysql_real_escape_string($this->title).'\',
				`text` = \''.mysql_real_escape_string($this->text).'\',
				`date` = \''.mysql_real_escape_string($this->date).'\'
			WHERE id = \''.mysql_real_escape_string($this->id).'\';');
			return $qry;
		}
	}
	
	/**
	 * Removes the model object's corresponding row in the database and returns true if successfull.
	 * @return boolean
	 */
	public function removeFromDB(){
		if(!is_null($this->id)){
			return mysql_query('DELETE FROM '.TABLE_PREFIX.'messages WHERE id = \''.mysql_real_escape_string($this->id).'\';');
		} else {
			return true;
		}
	}
	
/* ####### GETTER ####### */
	
	/**
	 * The Message's id
	 * @return int
	 */
	public function getID(){
		return $this->id;	
	}
	
	/**
	 * The linked Message's id
	 * @return int
	 */
	public function getMessageID(){
		return $this->messageID;	
	}
	
	/**
	 * The linked List's id
	 * @return int
	 */
	public function getListID(){
		return $this->listID;	
	}
	
	/**
	 * The linked Todo's id
	 * @return int
	 */
	public function getTodoID(){
		return $this->todoID;	
	}
	
	/**
	 * The linked User's id
	 * @return int
	 */
	public function getUserID(){
		return $this->userID;	
	}
	
	/**
	 * The author's id
	 * @return int
	 */
	public function getAuthorID(){
		return $this->authorID;	
	}
	
	/**
	 * The Message's title
	 * @return string
	 */
	public function getTitle(){
		return $this->title;	
	}
	
	/**
	 * The Message's text
	 * @return string
	 */
	public function getText(){
		return $this->text;	
	}
	
	/**
	 * The Message's date
	 * @return timestamp
	 */
	public function getDate(){
		return $this->date;	
	}
	
/* ####### SETTER ####### */
	
	/**
	 * Sets the Message's id
	 * @param int $id
	 */
	public function setID($id){
		$this->id = (int)$id;	
	}
	
	/**
	 * Sets the linked Message's id
	 * @param int $id
	 */
	public function setMessageID($id){
		$this->messageID = (int)$id;	
	}
	
	/**
	 * Sets the linked List's id
	 * @param int $id
	 */
	public function setListID($id){
		$this->listID = (int)$id;	
	}
	
	/**
	 * Sets the linked Todo's id
	 * @param int $id
	 */
	public function setTodoID($id){
		$this->todoID = (int)$id;	
	}
	
	/**
	 * Sets the linked User's id
	 * @param int $id
	 */
	public function setUserID($id){
		$this->userID = (int)$id;	
	}
	
	/**
	 * Sets the author's id
	 * @param int $id
	 */
	public function setAuthorID($id){
		$this->authorID = (int)$id;	
	}
	
	/**
	 * Sets the Message's title
	 * @param string $title
	 */
	public function setTitle($title){
		$this->title = $title;	
	}
	
	/**
	 * Sets the Message's text
	 * @param string $text
	 */
	public function setText($text){
		$this->text = $text;	
	}
	
	/**
	 * Sets the Message's date
	 * @param timestamp $date
	 */
	private function setDate($date){
		$this->date = (int)$date;	
	}
	
}

?>